/**
 * Copyright (C), 2015-2021, 广州同心日信息科技责任有限公司
 * FileName: DateTextView
 * Author: Administrator
 * Date: 2021/4/18 11:26
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.cmw.auntapp.component;


import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ExpandableListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

import com.cmw.auntapp.R;

import java.util.Calendar;

/**
 * @ClassName: DateTextView
 * @Description: 日期选择控件
 * @Author: Administrator
 * @Date: 2021/4/18 11:26
 * @Phone: 13922111489
 * @Email: 340360491@qq.com
 */
public class DateTextView extends AppCompatEditText {
    private final int DRAWABLE_LEFT = 0;
    private final int DRAWABLE_TOP = 1;
    private final int DRAWABLE_RIGHT = 2;
    private final int DRAWABLE_BOTTOM = 3;

    private DrawableLeftListener mLeftListener;
    private DrawableRightListener mRightListener;
    private DrawableTopListener mTopListener;
    private DrawableBottomListener mBottomListener;

    private DatePickerDialog datePickerDialog;


    public DateTextView(Context context) {
        super(context);
        init();
    }

    public DateTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setCursorVisible(false);
        setFocusable(false);
        setFocusableInTouchMode(false);
        Drawable drawable = getResources().getDrawable(R.drawable.calendar_24);
        setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
        setCompoundDrawablePadding(0);
        setDrawableRightListener(this::onDrawableRightClick);
        createDatePickerDialog();
    }

    private void createDatePickerDialog() {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar=Calendar.getInstance();
        //构建一个日期对话框，该对话框已经集成了日期选择器
        //DatePickerDialog的第二个构造参数指定了日期监听器
        datePickerDialog = new DatePickerDialog(getContext(), (DatePicker view, int year, int month, int dayOfMonth) -> {
            //获取日期选择器dp_date设定的年月份
            setValue(year, month, dayOfMonth);
        },
        calendar.get(Calendar.YEAR),
        calendar.get(Calendar.MONTH),
        calendar.get(Calendar.DAY_OF_MONTH));
    }

    private void setValue(int year, int month, int dayOfMonth) {
        month += 1;
        String currMonth = (month < 10) ? "0"+month : month+"";
        String currDayOfMonth = (dayOfMonth < 10) ? "0"+dayOfMonth : dayOfMonth+"";
        String dateVal = String.format("%s-%s-%s",year+"", currMonth, currDayOfMonth);
        this.setText(dateVal);//
    }

    private void onDrawableRightClick(View view) {
        //把日期对话框显示在界面上
        datePickerDialog.show();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_UP) {
            return super.onTouchEvent(event);
        }
        setDrawableClick(event);
        return super.onTouchEvent(event);
    }

    /**
     * event.getRawX(),相对于左边界的绝对坐标，以左上角为（0,0）
     * event.getX()，相对于自身的坐标，以该空间的左上角为（0,0）
     * getLeft()，相当于margin,控件左边界相对于父控件的距离
     * getPaddingLeft,相当于padding，控件中元素相对于控件的间距
     * getBounds().width()，获取元素绘制区域的宽度
     * drawableRight.getIntrinsicWidth()，获取drawable的实际宽度
     */
    private void setDrawableClick(MotionEvent event) {
        Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
        //右图标点击事件： 首先确保得到的drawable对象不为空，然后得到本次点击事件的x轴坐标，如果>当前控件宽度-控件右间距-drawable实际展示大小，
        if (mRightListener != null && drawableRight != null
                && event.getX() > (getWidth() - getPaddingRight() - drawableRight.getIntrinsicWidth())) {
            mRightListener.onDrawableRightClick(this);
        }

        // 左图标点击事件：
        Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
        if (mLeftListener != null && drawableLeft != null
                && event.getX() <= getLeft() + drawableLeft.getIntrinsicWidth()) {
            mLeftListener.onDrawableLeftClick(this);
        }

        //自己去理解，比较容易的
        if (mTopListener != null) {
            Drawable drawableTop = getCompoundDrawables()[DRAWABLE_TOP];
            if (drawableTop != null && event.getY() <= getTop() + drawableTop.getIntrinsicHeight()) {
                mTopListener.onDrawableTopClick(this);
            }
        }

        //自己去理解，比较容易的
        if (mBottomListener != null) {
            Drawable drawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
            if (drawableBottom != null && event.getX() > getHeight() - drawableBottom.getIntrinsicWidth()) {
                mBottomListener.onDrawableBottomClick(this);
            }
        }
    }

    //相应的控件注册的事件
    public void setDrawableLeftListener(DrawableLeftListener mListener) {
        this.mLeftListener = mListener;
    }

    //相应的控件注册的事件
    public void setDrawableRightListener(DrawableRightListener mListener) {
        this.mRightListener = mListener;
    }

    //相应的控件注册的事件
    public void setDrawableTopListener(DrawableTopListener mListener) {
        this.mTopListener = mListener;
    } //相应的控件注册的事件

    public void setDrawableBottomListener(DrawableBottomListener mListener) {
        this.mBottomListener = mListener;
    }




    /**
     * 回调接口需要，需要复写
     */
    public interface DrawableRightListener {
        void onDrawableRightClick(View view);
    }

    /**
     * 回调接口
     */
    public interface DrawableLeftListener {
        void onDrawableLeftClick(View view);
    }

    /**
     * 回调接口
     */
    public interface DrawableTopListener {
        void onDrawableTopClick(View view);
    }

    /**
     * 回调接口
     */
    public interface DrawableBottomListener {
        void onDrawableBottomClick(View view);
    }

}